<template>
  <el-container id="one">
    <router-view></router-view>
    <div id="two">
      <!-- 面包屑导航区 -->
      <!-- 面包屑导航区 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/teacher/welcome' }" @click.native="saveNavState('/teacher/welcome')">首页</el-breadcrumb-item>
        <el-breadcrumb-item>选课管理</el-breadcrumb-item>
        <el-breadcrumb-item>选课信息管理</el-breadcrumb-item>
      </el-breadcrumb>
      <br />
      <el-card>
        <el-row :gutter="20">
          <el-col :span="4">
            <el-select v-model="queryInfo.courseType" placeholder="请选择课程类别">
              <el-option v-for="item in leibie" :key="item.type" :label="item.type" :value="item.type"></el-option>
            </el-select>
          </el-col>
          <el-col :span="6">
            <el-input placeholder="请输入查找内容" clearable v-model="queryInfo.query" @clear="clearValues">
              <el-button slot="append" icon="el-icon-search" @click="getSelectionListLike()"></el-button>
            </el-input>
          </el-col>
          <el-col :span="3">
            <el-button type="info" @click="clearValues">清除搜索</el-button>
          </el-col>
        </el-row>
        <!-- 选课信息列表区域 -->
        <el-table :data="selectionList" ref="table" boder stripe :height="tableHeight">
          <el-table-column align="center" type="index" label="#"></el-table-column>
          <el-table-column align="center" prop="courseId" label="课程ID"></el-table-column>
          <el-table-column align="center" prop="name" label="课程名称"></el-table-column>
          <el-table-column align="center" prop="courseType" label="课程类别"></el-table-column>
          <el-table-column align="center" prop="courseCount" label="已选人数"></el-table-column>
          <el-table-column align="center" label="操作" width="300px">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-s-fold" size="mini" @click="showDetailsDialog(scope.row.id)">课程详情</el-button>
              <el-button type="primary" icon="el-icon-s-unfold" size="mini" @click="showSelectDialog(scope.row.id)" :disabled="scope.row.count == '0'">选课详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 信息条数提示 -->
        <el-pagination layout="total" :total="totle"></el-pagination>
      </el-card>
      <!-- 课程详情对话框 -->
      <el-dialog center title="课程详情" :visible.sync="detailsDialogVisible" width="40%">
        <el-form :model="detailsForm" label-width="80px">
          <el-form-item label="课程ID" prop="courseId">
            <el-input disabled v-model="detailsForm.courseId"></el-input>
          </el-form-item>
          <el-form-item label="课程名称" prop="name">
            <el-input disabled v-model="detailsForm.name"></el-input>
          </el-form-item>
          <el-form-item label="课程类别" prop="courseType">
            <el-input disabled v-model="detailsForm.courseType"></el-input>
          </el-form-item>
          <el-form-item label="课程学分" prop="courseCore">
            <el-input disabled v-model="detailsForm.courseCore"></el-input>
          </el-form-item>
          <!-- <el-form-item label="开课时间" prop="cTime">
            <el-input disabled v-model="detailsForm.cTime"></el-input>
          </el-form-item> -->
          <el-form-item label="上课教室" prop="classRoom">
            <el-input disabled v-model="detailsForm.classRoom"></el-input>
          </el-form-item>
          <el-form-item label="开始周次" prop="startWeek">
            <el-input disabled v-model="detailsForm.startWeek"></el-input>
          </el-form-item>
          <el-form-item label="结束周次" prop="endWeek">
            <el-input disabled v-model="detailsForm.endWeek"></el-input>
          </el-form-item>
          <el-form-item label="上课周次" prop="weekTime">
            <el-input disabled v-model="detailsForm.weekTime"></el-input>
          </el-form-item>
          <el-form-item label="上课节次" prop="classTime">
            <el-input disabled v-model="detailsForm.classTime"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="detailsDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="goCurri">前往修改</el-button>
        </span>
      </el-dialog>
      <!-- 选课详情对话框:显示当前课程的所有选课学生信息 -->
      <el-dialog center title="选课详情" :visible.sync="selectDialogVisible" width="90%">
        <el-table :data="selectList" ref="table" border stripe>
          <el-table-column align="center" type="index" label="#"></el-table-column>
          <el-table-column align="center" prop="userId" label="学号"></el-table-column>
          <el-table-column align="center" prop="studentName" label="姓名"></el-table-column>
          <el-table-column align="center" prop="sex" label="性别"></el-table-column>
          <el-table-column align="center" prop="college" label="学院"></el-table-column>
          <el-table-column align="center" prop="major" label="专业"></el-table-column>
          <el-table-column align="center" prop="className" label="班级"></el-table-column>
          <el-table-column align="center" prop="xuankeTime" label="选课时间"></el-table-column>
        </el-table>
        <span slot="footer" class="dialog-footer">
          <el-button @click="selectDialogVisible = false">取 消</el-button>
        </span>
      </el-dialog>
    </div>
  </el-container>
</template>

<script>
export default {
  name: 'CourseSelection',
  data() {
    return {
      tableHeight: 100,
      queryInfo: {
        pageSize: 5,
        pageNum: 1,
        query: '',
        courseType: ''
      },
      selectionList: [],
      totle: 0,
      leibie: [
        {
          id: 0,
          type: ''
        },
        {
          id: 1,
          type: '必修'
        },
        {
          id: 2,
          type: '选修'
        }
      ],
      detailsForm: {},
      detailsDialogVisible: false,
      selectList: [],
      selectDialogVisible: false
    }
  },
  mounted: function () {
    this.$nextTick(function () {
      this.tableHeight = window.innerHeight - this.$refs.table.$el.offsetTop - 95
      let self = this
      window.onresize = function () {
        self.tableHeight = window.innerHeight - self.$refs.table.$el.offsetTop - 95
      }
    })
  },
  created() {
    this.saveNavState('/teacher/course')
    this.queryInfo.query = ''
    this.getSelectionList()
  },
  methods: {
    // 跳转到修改课程信息界面
    goCurri() {
      window.sessionStorage.setItem('tag', '0')
      window.sessionStorage.setItem('classid', this.detailsForm.courseId.toString())
      window.sessionStorage.setItem('activePath', '/teacher/curriculum')
      this.detailsDialogVisible = false
      this.$router.push('/teacher/curriculum')
      this.$router.go(0)
    },
    // 课程详情对话框
    showDetailsDialog(id) {
      this.$axios.get('teacher/getcurri/' + id).then((res) => {
        if (res.status !== 200) {
          return this.$message.error('获取课程信息失败！')
        } else {
          this.detailsForm = res.data.data
          this.detailsDialogVisible = true
        }
      })
    },
    // 选课详情对话框
    showSelectDialog(courseId) {
      this.$axios.get('teacher/getallselection/' + courseId).then((res) => {
        if (res.status !== 200) {
          return this.$message.error('获取课程详情信息失败!')
        } else {
          this.selectList = res.data.data
          this.selectDialogVisible = true
        }
      })
    },
    // 对课程名称进行模糊查询
    getSelectionListLike() {
      this.getSelectionList()
    },
    // 清除数据
    clearValues() {
      this.queryInfo.query = ''
      this.queryInfo.courseType = ''
      this.getSelectionList()
    },
    // 获取选课情况信息列表
    getSelectionList() {
      this.$axios
        .post('teacher/getselection', {
          userId: window.sessionStorage.getItem('userId'),
          query: this.queryInfo.query,
          courseType: this.queryInfo.courseType,
          pageSize: this.queryInfo.pageSize,
          pageNum: this.queryInfo.pageNum
        })
        .then((res) => {
          if (res.status !== 200) {
            return this.$message.error('获取选课信息列表失败！')
          }
          this.selectionList = res.data.data.list
          this.totle = res.data.data.list.length
        })
    },
    // 保存链接的激活地址
    saveNavState(activePath) {
      this.resetSetItem('activePath', activePath)
      // window.sessionStorage.setItem('activePath',activePath)
    }
  }
}
</script>

<style scoped="scoped">
#one {
  width: 100%;
  height: auto;
}
#two {
  width: 100%;
  height: 100%;
}
.el-card {
  height: 100%;
  width: 100%;
}
.el-table {
  padding-top: 20px;
}
.el-pagination {
  text-align: left;
  padding-top: 20px;
}
</style>
